// pages/mine/mine.js
import urls from '../../utils/urls.js'
import loginFn from '../../utils/login.js'
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		showauth: true, //显示授权 
		userInfo: '',
		myInfo: '', //个人信息
		studentList: [], //学生列表
		grate: []
	},
	mysignIn: function() {
		wx.navigateTo({
			url: '/pages/signin/signin',
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		console.log('options:',options);
		var userInfo = wx.getStorageSync('userinfo')
		if (userInfo) {
			this.setData({
				showauth: false,
				userInfo: userInfo
			})
		};


	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		this.getmyInfoFn() //获取个人信息
		if (this.data.grate.length > 0) {
			this.getstudentlist()
		} else {
			this.getgradeFn()
		};

	},


	getmyInfoFn: function() { //获取个人中心
		var that = this;
		var loginInfo = loginFn()
		wx.request({
			url: urls.userinfo,
			method: 'POST',
			data: loginInfo,
			success: function(res) {
				console.log(res)
				if (res.data.code == '0') {
					that.setData({
						myInfo: res.data.data
					})
				};
			}
		})
	},
	addStudentFn: function(e) { //添加学生
		wx.navigateTo({
			url: '/pages/addstudent/index',
		})
	},
	addclassFn: function(e) { //
		wx.navigateTo({
			url: '/pages/addclass/index',
		})
	},
	//去机构管理端
	agentFns: function(e) {
		wx.navigateTo({
			url: '/pages/institucharge/institucharge',
		})
	},
	//去老师管理端
	agentFn: function(e) {
		wx.navigateTo({
			url: '/pages/teacher-info/index',
		})
	},
	getpromotion: function() { //获取优惠券
		wx.navigateTo({
			url: '/pages/coupons/index?getpromotion=true',
		})
	},
	mycardFn: function() { //我的卡券
		wx.navigateTo({
			url: '/pages/coupons/index?mine=true',
		})
	},
	myorderFn: function(e) { //我的订单
		console.log(e)
		wx.navigateTo({
			url: '/pages/myorder/index?status=' + e.currentTarget.dataset.status,
		})
	},
	loginSuccess: function(e) {
		console.log('安排')
		console.log(e)
	},
	getInfoFn: function(e) { //获取信息
		console.log(e.detail)
		var that = this;
		var loginInfo = wx.getStorageSync('login')
		if (e.detail.userInfo) {
			wx.setStorageSync('userinfo', e.detail.userInfo)
			this.setData({
				showauth: false
			})
			var datas = e.detail.userInfo
			loginInfo.nick = datas.nickName
			loginInfo.avatar = datas.avatarUrl
			loginInfo.city = datas.city
			wx.request({
				url: urls.usersave,
				method: 'POST',
				data: loginInfo,
				success: function(res) {

				}
			})
		};

	},
	testFn: function(e) { //点击开始测试
		var year = 1000 * 60 * 60 * 24 * 365;
		var now = new Date();
		var birth = e.currentTarget.dataset.birthday;
		birth = Date.parse(birth.replace('/-/g', "/"));
		var birthday = new Date(birth);
		var age = parseInt((now - birthday) / year);
		var questionTypeId = 1
		if (age > 6) {
			questionTypeId = 2
		};
		wx.navigateTo({
			url: '/pages/test/index?studentId=' + e.currentTarget.dataset.id + '&questionTypeId=' + questionTypeId,
			success: function(res) {},
			fail: function(res) {},
			complete: function(res) {},
		})
	},
	getstudentlist: function() { //获取学生列表
		var that = this;
		var loginInfo = loginFn()
		wx.request({
			url: urls.studentlist,
			method: 'POST',
			data: loginInfo,
			success: function(res) {
				if (res.data.code == '0') {
					var data = res.data.data;
					for (var i = 0; i < that.data.grate.length; i++) {
						for (var j = 0; j < data.length; j++) {
							if (data[j].gradeId == that.data.grate[i].gradeId) {
								data[j]['grate'] = that.data.grate[i].grade
							};
						}
					}
					console.log(data)
					that.setData({
						studentList: data
					})
				};
			}
		})
	},
	getgradeFn: function() { //获取年级
		var that = this;
		var loginInfo = loginFn()
		wx.request({
			url: urls.studentGrade,
			method: 'POST',
			data: loginInfo,
			success: function(res) {
				if (res.data.code = '0') {
					that.setData({
						grate: res.data.data.grade
					})
					that.getstudentlist()
				}
			}
		})
	},
	editstudent: function(e) { //编辑学生
		wx.navigateTo({
			url: '/pages/editstudent/index?studentId=' + e.currentTarget.dataset.id + '&idx=' + e.currentTarget.dataset.idx,
		})
	}


})
